﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ImageParser
{
    public enum WallType
    {
        NoneWall = 1,
        AllWall = 2,
        RightWall = 3,
        RightBotWall = 4,
        BotWall = 5,
        LeftBotWall = 6,
        LeftWall = 7,
        TopLeftWall = 8,
        TopWall = 9,
        TopRightWall = 10,
        TopBotWall = 11,
        TopLeftBotWall = 12,
        TopRightBotWall = 13,
        LeftRightWall = 14,
        LeftBotRightWall = 15,
        LeftTopRightWall = 16
    }
    class Program
    {
        string imagePath = @"C:\Unity Projects\Cubie\Cubie\LevelEditor\Texture\testWall.png";
        string outputPath = @"C:\Unity Projects\Cubie\Cubie\LevelEditor\Texture\testWalloutput.txt";
        Bitmap image;
        private int cubeSize = 10;
        int height, width;
        Color wallColor = Color.FromArgb(27, 135, 158);
        Color nullColor = Color.FromArgb(0, 255, 255, 255);
        
        static void Main(string[] args)
        {
            new Program().ParseImage();
        }

        public void ParseImage()
        {
            image = new Bitmap(imagePath);
            height = image.Height;
            width = image.Width;
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
            {

                for (int i = cubeSize / 2; i < height; i += cubeSize)
                {
                    string line = "";
                    for (int j = cubeSize / 2; j < width; j += cubeSize)
                    {
                        Color clr = image.GetPixel(j, i);
                        
                        if (clr.Equals(wallColor))
                        {
                            line += " " + GetWallType(clr, j < cubeSize ? nullColor : image.GetPixel(j - cubeSize, i),
                                j > width - cubeSize ? nullColor : image.GetPixel(j + cubeSize, i),
                                i < cubeSize ? nullColor : image.GetPixel(j, i - cubeSize),
                                i > height - cubeSize ? nullColor : image.GetPixel(j, i + cubeSize));
                        }
                        else if(clr.A == 0)
                        {
                            line += " 0";
                        }
                    }
                    file.WriteLine(line);
                }
                file.Close();
            }
        }

        private String GetWallType(Color thisColor, Color leftUnit, Color rightUnit, Color topUnit, Color botUnit) 
        {
            WallType result = WallType.NoneWall;
            if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && thisColor.Equals(botUnit))
                result = WallType.NoneWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(botUnit))
                result = WallType.BotWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.RightWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit))
                result = WallType.LeftWall;
            else if (thisColor.Equals(rightUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopLeftWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopRightWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(botUnit))
                result = WallType.RightBotWall;
            else if (thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.LeftBotWall;
            else if (thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopLeftBotWall;
            else if (thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopRightBotWall;
            else if (thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.LeftBotRightWall;
            else if (thisColor.Equals(botUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit))
                result = WallType.LeftTopRightWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopBotWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.LeftRightWall;
            else if(!thisColor.Equals(topUnit) && !thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.AllWall;
            return ((int)result).ToString(); 
        }
    }
}
